// pages/Classv/Classv.js
const Aotu = require('../../utils/auth')
const {
  GoShop
} = require('../../utils/api')
const {
  Fen,
  Shoplist,
  Detail,
  PeiShop
} = require('../../utils/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ClassArr: [],
    Right: [],
    value: '',
    Index: 339312,
    Flag: false,
    show: false,
    Ti: [],
    Ids: 0,
    Ids1: 0,
    properties: [],
    pei: [],
    PeijianId: [],
    flags: false,
    flags1: false,
    SUk: [],
    GoodsId: 0,
    Cun: 0,
    SukId: 0,
    Num:1,
  },
  getlist() {
    Fen().then((res) => {
      console.log(res);
      this.setData({
        ClassArr: res
      })
    })

  },
  // 加入购物车
  Goshop(e) {
    console.log(e.target.dataset.obj);
    GoShop({
      goodsId: e.target.dataset.obj.id,
      number: e.target.dataset.obj.minBuyNumber,
      token: wx.getStorageSync('token')
    }).then((res) => {
    //   console.log(res);
    console.log(res);
    //   wx.showToast({
    //     title: '成功加入购物车',
    //     url:'/pages/Shopping Cart/Shopping Cart'
    //   })
    })
    // minBuyNumber

  },
  // 加入购物车2有规格的
  Shops() {
    let number = 1
    let goodsId = this.data.GoodsId * 1
    let addition = this.data.PeijianId
    let token = wx.getStorageSync('token')
    let sku = this.data.SUk
    console.log(goodsId);
    console.log(this.data.PeijianId);
    console.log(this.data.SUk);
    console.log(wx.getStorageSync('token'));
    GoShop({
      goodsId,
      number,
      addition,
      sku,
      token
    }).then((res) => {
      console.log(res);
      wx.showToast({
        title: '成功加入购物车',
      })
      this.onClose()
    })
  },



  // 选取规格样式
  GuiGe1(e) {
    let that = this
    let selectIndex = e.currentTarget.dataset.selectIndex;
    let attrIndex = e.currentTarget.dataset.attrIndex;
    let content = e.currentTarget.dataset.content
    var count = content[selectIndex * 1].childsCurGoods.length
    var Conflag = content.length
    var Sukflag
    console.log(this.data.Ti);
    // 循环第一层给它一个状态
    for (var i = 0; i < count; i++) {
      content[selectIndex * 1].childsCurGoods[i].isSelect = false
    }
    this.data.Cun = content[selectIndex * 1].childsCurGoods[attrIndex].id
    content[selectIndex * 1].childsCurGoods[attrIndex].isSelect = true;
    // 父元素状态
    // content[selectIndex * 1].isSelect = true
    const {
      propertyId,
      id
    } = content[selectIndex * 1].childsCurGoods[attrIndex]
    let obj = {
      optionId: propertyId,
      optionValueId: id
    }
    this.data.SUk.push(obj)
    // let Indexs = this.data.SUk.findIndex(item => item.optionValueId == content[selectIndex * 1].childsCurGoods[attrIndex].id)
    // this.data.SUk.splice(Indexs, 1)
    console.log(this.data.SUk);

    that.setData({
      properties: content
    })
  },





  // 商品配件
  GuiGe2(e) {
    let that = this
    let selectIndex = e.currentTarget.dataset.selectIndex;
    let attrIndex = e.currentTarget.dataset.attrIndex;
    let content = e.currentTarget.dataset.content
    var count = content.items.length
    if (this.data.flags == false) {
      // 循环第一层给它一个状态
      for (var i = 0; i < count; i++) {
        content.items[i].isSelect = false
      }
      content.items[attrIndex].isSelect = true;
      this.data.flags = true
      console.log(this.data.flags);
      const {
        id,
        pid
      } = content.items[attrIndex]
      let obj = {
        id: id,
        pid: pid
      }
      this.data.PeijianId.push(obj)
    } else {
      content.items[attrIndex].isSelect = false;
      console.log(this.data.Ti);
      console.log(this.data.flags);
      this.data.flags = false
      this.data.PeijianId.splice(0, 1)
      console.log(this.data.PeijianId);

    }
    console.log(content.items[attrIndex]);
    that.setData({
      pei: content,
      PeijianId: this.data.PeijianId
    })
    console.log(this.data.PeijianId);
  },
  // 搜索
  search(e) {
    console.log(e.detail);
    wx.redirectTo({
      url: `/pages/Shoplist/Shoplist?obj=${this.data.value}`,
    })
  },
  // 切换
  Cut(e) {
    this.setData({
      Index: e.target.id
    })
    // console.log(e.target.id);

    Shoplist({
      categoryId: this.data.Index
    }).then((res) => {
      console.log(res);
      var vc = false
      if (!res.code) {
        vc = false
      } else {
        vc = true
      }
      this.setData({
        Right: res.result,
        Flag: vc
      })
    })
  },
  // 计数器
  onChange(e) {
    console.log(e);
    this.setData({ Num:e.detail});
  },
  // 唤起弹框
  Adds(e) {
    console.log(e);
    PeiShop({
      goodsId: e.target.id,
      token: wx.getStorageSync('token')
    }).then((res) => {
      // console.log(res);
      this.setData({
        pei: res[0]
      })
      console.log(this.data.pei);
    })
    Detail({
      id: e.target.id
    }).then((res) => {
      console.log(res);
      this.setData({
        show: true,
        Ti: res,
        properties: res.properties,
        GoodsId: e.target.id
      })
    })


    console.log(e.target.dataset.obj);
  },
  // 关闭弹框
  onClose() {
    this.setData({
      show: false
    })
    this.data.SUk = [],
      this.data.PeijianId = []
      this.data.Num=1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  //   点击主页分类跳转数据
  onLoad(options) {
    console.log(options.id);
    // console.log(this.data.Index);
    let Index = options.id
    Shoplist({
      categoryId: Index || this.data.Index
    }).then((res) => {
      console.log(res);
      var vc = false
      if (!res.code) {
        vc = false
      } else {
        vc = true
      }
      this.setData({
        Right: res.result,
        Flag: vc,
        Index: Index,
      })
    })
    this.getlist()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


    console.log('OnShow');

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log('pull down.');
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})